package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.CheckMobileOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 实现号段补全过滤策略
 *  <bean id="phase"  class="com.qf.service.impl.PhaseFilterServiceImpl"/>
 * @author lixu
 */
@Service("phase")
public class PhaseFilterServiceImpl  implements FilterChainService {


    @Autowired
    private CacheFeign cacheFeign;

    /**
     *   //1:号段补全实现类
     *   //**号段补全 : **从号段表中查询手机所在区域补全手机号段信息，号段信息可从REDIS缓存中取，找不到号段信息就填0，表示未知。
     * @param submit
     * @return
     */
    @Override
    public boolean strategy(StandardSubmit submit) {
        Integer clientID = submit.getClientID();
        //号段补全代码
        // keys  ROUTER:2:*
        String cacheKeyRouter = CacheConstants.CACHE_PREFIX_ROUTER + clientID + ":*";
        /**
         * 1) "ROUTER:6:2"
         * 2) "ROUTER:6:1"
         * 3) "ROUTER:6:3"
         * 4) "ROUTER:6:4"
         */
        Set<String> keyList = cacheFeign.keys(cacheKeyRouter);
        // 9 8 10 11  通道 的Id
        Set<String> ks = keyList.stream().map(value -> {
            String[] v = value.split(":");
            return v[2];
        }).collect(Collectors.toSet());
        //通道的缓存Key
        String cacheKeyChannel = CacheConstants.CACHE_PREFIX_SMS_CHANNEL;

        String p = null;

        //1:运营商     0 - 未知,1 - 移动, 2 - 联通 ,3 - 电信
        String destMobile = submit.getDestMobile();
        if(CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            //是移动
            submit.setOperatorId(OperatorConstants.CHINA_MOBILE);
            cacheKeyChannel+= OperatorConstants.CHINA_MOBILE + ":";
            p = "中国移动";
        } else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)) {
            //是联通
            submit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            cacheKeyChannel+= OperatorConstants.CHINA_UNICOM + ":";
            p = "中国联通";
        } else if (CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)) {
            //是电信
            submit.setOperatorId(OperatorConstants.CHINA_TELECOM);
            cacheKeyChannel+= OperatorConstants.CHINA_TELECOM + ":";
            p = "中国电信";
        }else{
            //是未知
            submit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            cacheKeyChannel+= OperatorConstants.CHINA_UNKNOWN + ":";
            p = "中国未知";
        }
        //PHASE:1300011
        String cacheKey = CacheConstants.CACHE_PREFIX_PHASE + destMobile.substring(0,7);
        //"2,33"
        String value = cacheFeign.get(cacheKey);
        if(StringUtils.isEmpty(value)){
            submit.setProvinceId(0);
            submit.setCityId(0);
        }else{
            String[] vs = value.split(",");
            //2:省
            submit.setProvinceId(Integer.parseInt(vs[0]));
            //3:市
            submit.setCityId(Integer.parseInt(vs[1]));
        }
        //4:       channel:1:
        for (String k : ks) {
            //cacheKeyChannel+=k;   //channel:1:1:2:3:4      channel:1:1  channel:1:2
            Map map = cacheFeign.hMGet(cacheKeyChannel + k);
            String channelname = (String) map.get("channelname");
            if(p.equals(channelname)){
                //下发网关ID号
                submit.setGatewayID(Integer.parseInt(k));
                break;
            }
        }
        //5:下发的源号码  spNumber（channel表） + extendNumber（client_channel表）
        String cacheKeySpNumber = CacheConstants.CACHE_PREFIX_SMS_CHANNEL
                + submit.getOperatorId() + ":" + submit.getGatewayID();
        //spNumber（channel表）  缓存中： CHANNEL:通道的类型:通道的ID
        String spNumber = cacheFeign.hget(cacheKeySpNumber, "spnumber");
        //ROUTER:6:4  ROUTER:客户ID:通道的ID
        String cacheKeyextendNumber = CacheConstants.CACHE_PREFIX_ROUTER + clientID + ":" + submit.getGatewayID();
        String extendNumber = (String) cacheFeign.hget(cacheKeyextendNumber,"extendnumber");
        String srcNumber = spNumber  + extendNumber;
        submit.setSrcNumber(srcNumber);
        // 短信：   1069886   中国移动  千锋互联发送给用户的验证码的短信（发出号 1069886
        //         1069887   中国联通
        //   1069886 9988:登录验证码
        //   1069886 9977：支付验证码
        //成功
        return true;
    }
}
